﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpAlgorithm
{
    class Survivor
    {
        public Survivor()
        {
            int testCount = Int32.Parse(TestConsole.ReadLine());

            for (int testCase = 1; testCase <= testCount; testCase++)
            {
                int foodCount = Int32.Parse(TestConsole.ReadLine());
                List<int[]> foodList = new List<int[]>();
                for (int i = 1; i <= foodCount; i++)
                {
                    string[] input = TestConsole.ReadLine().Split(' ');
                    int start = Int32.Parse(input[0]);
                    int delay = Int32.Parse(input[1]);
                    int end = start + delay;
                    foodList.Add(new int[3] { start, delay, end });
                }
                foodList = foodList.OrderBy(f => f[0]).ToList();
                int endMax = foodList.Max(f => f[2]);
                int survivor = 0;
                while (foodList.Count > 0)
                {
                    int[] eating = foodList[0]; //앞으로 먹을 음식 후보
                    if (eating[0] >= survivor) //아직 유통기한이 남았을 경우
                    {
                        survivor += eating[1];
                    }
                    else if (eating[2] > survivor) //미리 먹었을때 아직 버틸수 있었던 음식
                    {
                        survivor = eating[2];
                    }
                    foodList.RemoveAt(0);
                }
                Console.WriteLine("Case #" + testCase + ": " + survivor);
            }
        }
    }
}
